import sys

file = open("matrix.txt", "r")
array = [[int(x) for x in line.split(",")] for line in file]

#array = [[131, 673, 234, 103, 18],
#         [201, 96, 342, 965, 150],
#         [630, 803, 746, 422, 111],
#         [537, 699, 497, 121, 956],
#         [805, 732, 524, 37, 331]]
rows = len(array)
columns = len(array[0])

minsums = [[0 for c in xrange(columns)] for r in xrange(rows)]

def getminsum(row, column):
    if row < 0 or row >= rows: return sys.maxint
    if column < 0 or column >= columns: return sys.maxint    
    return minsums[row][column]

for row in xrange(0, rows):
    minsums[row][0] = array[row][0]
    
for column in xrange(1, columns):
    for row in xrange(0, rows):
        minsums[row][column] = minsums[row][column-1] + array[row][column]
    
    changed = True
    while changed:
        changed = False
        
        for row in xrange(0, rows):
            prev = minsums[row][column]
            
            fromtop = getminsum(row-1,column) + array[row][column]
            if fromtop < prev:
                minsums[row][column] = fromtop
                changed = True

            frombottom = getminsum(row+1, column) + array[row][column]
            if frombottom < prev:
                minsums[row][column] = frombottom
                changed = True

print min(minsums[row][columns - 1] for row in xrange(0, rows))
        

